﻿#include "LampToolBar.h"
#include <QVBoxLayout>
#include "LampControl/B3PanelTestLamp.h"

LampToolBar::LampToolBar(QWidget *parent)
    :QToolBar(parent)
{
    setIconSize(QSize(48, 48));
    initContent();
    initConnection();
}
void LampToolBar::setOrientation(Qt::Orientation orientation)
{
    if(sliderTop) sliderTop->setOrientation(orientation);
    if(sliderBottom)  sliderBottom->setOrientation(orientation);
}

void LampToolBar::initContent()
{
//    verticalLayout = new QVBoxLayout(this);
//    verticalLayout->setObjectName(QStringLiteral("verticalLayout"));

    labelTopName = new QLabel(this);
    labelTopName->setObjectName(QStringLiteral("labelTopName"));
    labelTopName->setText(u8"上光源");
//    verticalLayout->addWidget(labelTopName);
    addWidget(labelTopName);

    sliderTop = new QSlider(this);
    sliderTop->setObjectName(QStringLiteral("verticalSliderTop"));
    sliderTop->setMinimum(0);
    sliderTop->setMaximum(255);
    sliderTop->setOrientation(orientation());
    sliderTop->setMaximumHeight(300);
    sliderTop->setMaximumWidth(300);
//    verticalLayout->addWidget(verticalSliderTop);
    addWidget(sliderTop);

    labelTopValue = new QLabel(this);
    labelTopValue->setObjectName(QStringLiteral("labelTopValue"));
    labelTopValue->setText("0");
//    verticalLayout->addWidget(labelTopValue);
    addWidget(labelTopValue);

    addSeparator();

    labelBottomName = new QLabel(this);
    labelBottomName->setObjectName(QStringLiteral("labelTopName"));
    labelBottomName->setText(u8"下光源");
//    verticalLayout->addWidget(labelBottomName);
addWidget(labelBottomName);

    sliderBottom = new QSlider(this);
    sliderBottom->setObjectName(QStringLiteral("verticalSliderBottom"));
    sliderBottom->setMinimum(0);
    sliderBottom->setMaximum(255);
    sliderBottom->setOrientation(orientation());
    sliderBottom->setMaximumHeight(300);
    sliderBottom->setMaximumWidth(300);
//    verticalLayout->addWidget(verticalSliderBottom);
    addWidget(sliderBottom);

    labelBottomValue = new QLabel(this);
    labelBottomValue->setObjectName(QStringLiteral("labelBottomValue"));
    labelBottomValue->setText("0");
//    verticalLayout->addWidget(labelBottomValue);
    addWidget(labelBottomValue);


//    setLayout(verticalLayout);
    setWindowTitle("LampToolbar");
}

void LampToolBar::initConnection()
{
    connect(sliderTop, &QSlider::valueChanged, this, &LampToolBar::setTopLampIntensity);
    connect(sliderBottom, &QSlider::valueChanged, this, &LampToolBar::setBottomLampIntensity);
    connect(this, &QToolBar::orientationChanged, this, &LampToolBar::setOrientation);
}

void LampToolBar::updateTopLampIntensity(int value)
{
    int v = sliderTop->value();
    if(v != value)
    {
        sliderTop->setValue(value);
    }
}

void LampToolBar::updateBottomLampIntensity(int value)
{
    int v = sliderBottom->value();
    if(v != value)
    {
        sliderBottom->setValue(value);
    }
}
void LampToolBar::setTopLampIntensity(int value)
{
    if(m_pTopLamp)
    {
        m_pTopLamp->setValue(value);
        int value = m_pTopLamp->value();
        labelTopValue->setNum(value);
    }
}

void LampToolBar::setBottomLampIntensity(int value)
{
    if(m_pBottomLamp)
    {
        m_pBottomLamp->setValue(value);
        int value = m_pBottomLamp->value();
        labelBottomValue->setNum(value);
    }
}

void LampToolBar::attach(LampControl *top, LampControl *bottom)
{
    m_pTopLamp = top;
    m_pBottomLamp = bottom;

    if(m_pTopLamp) connect(m_pTopLamp, &LampControl::valueChanged, this, &LampToolBar::updateTopLampIntensity);
    if(m_pBottomLamp) connect(m_pBottomLamp, &LampControl::valueChanged, this, &LampToolBar::updateBottomLampIntensity);
}
